package com.iMusic.presentationLayer;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.iMusic.businessLogicLayer.Account;
import com.iMusic.businessLogicLayer.Product;
import com.iMusic.util.UserMessages;

public class SearchProduct extends HttpServlet {

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		HttpSession session = request.getSession();
		Account account = (Account) session.getAttribute("Account");
		String query = (String) request.getParameter("query");
		String redirection = "default.jsp";
		
		session.removeAttribute("Error");
		session.removeAttribute("ProductList");
		
		if (query != null && !query.equals("")) {

			try {
				List<Product> prodList = Product.searchByKeywords(account, query);
				
				session.setAttribute("ProductList", prodList);
				
				
			} catch (SQLException e) {
				session.setAttribute("Error", UserMessages.SEARCH_NOK);
				redirection += "?error="+UserMessages.SEARCH_NOK;
				e.printStackTrace();
			} 
			
			
			
		}
		
		
		
		response.sendRedirect(redirection);
		
	}
}
